<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改课题')" />
</head>
<body>
    <div class="main-content">
        <form class="form-horizontal m" id="form-topic-edit" th:object="${topic}">
            <h4 class="form-header h4">课题信息</h4>
            <input name="topicId" th:field="*{topicId}" type="hidden">
            
            <div class="row">
            	<div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label is-required">课题名称：</label>
                        <div class="col-sm-8">
                        	<input name="topicName" th:field="*{topicName}" class="form-control" type="text" maxlength="100"  required>
                        </div>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">课题编码：</label>
                        <div class="col-sm-8">
                            <input name="topicCode" th:field="*{topicCode}" class="form-control" maxlength="10" type="text">
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-12">
                    <div class="form-group">
                        <label class="col-xs-2 control-label">备注：</label>
                        <div class="col-xs-10">
                            <textarea name="remark" maxlength="200" class="form-control" rows="3">[[*{remark}]]</textarea>
                        </div>
                    </div>
                </div>
            </div>
            <h4 class="form-header h4">项目方案组信息</h4>
            <div class="row">
                <div class="col-sm-12">
                    <button type="button" class="btn btn-white btn-sm" onclick="addColumn()"><i class="fa fa-plus"> 增加</i></button>
                    <button type="button" class="btn btn-white btn-sm" onclick="sub.delColumn()"><i class="fa fa-minus"> 删除</i></button>
                    <button type="button" class="btn btn-white btn-sm" onclick="configPlan()"><i class="fa fa-list-ul"> 项目配置</i></button>
                    <div class="col-sm-12 select-table table-striped">
                        <table id="bootstrap-table"></table>
                    </div>
                </div>
            </div>
        </form>
    </div>
    
    <div class="row">
        <div class="col-sm-offset-5 col-sm-10">
            <button type="button" class="btn btn-sm btn-primary" onclick="submitHandler()"><i class="fa fa-check"></i>保 存</button>&nbsp;
            <button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i class="fa fa-reply-all"></i>关 闭 </button>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    
    <script th:src="@{/js/jquery.tmpl.js}"></script>
    <script th:inline="javascript">
        var prefix = ctx + "kt/topic";
        $("#form-topic-edit").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.saveTab(prefix + "/edit", $('#form-topic-edit').serialize());
            }
        }

        $(function() {
            var options = {
                data: [[${topic.topicPlanList}]],
                pagination: false,
                showSearch: false,
                showRefresh: false,
                showToggle: false,
                showColumns: false,
                sidePagination: "client",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'index',
                    align: 'center',
                    title: "序号",
                    formatter: function (value, row, index) {
                    	var columnIndex = $.common.sprintf("<input type='hidden' name='index' value='%s'>", $.table.serialNumber(index));
                    	return columnIndex + $.table.serialNumber(index);
                    }
                },
                {
                    field: 'planSort',
                    align: 'center',
                    title: '顺序',
                    width: '40px',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' type='text' name='topicPlanList[%s].planSort' value='%s' readonly><input type='hidden' name='topicPlanList[%s].planId' value='%s' >", index,  index+1,index,row.planId);
                        return html;
                    }
                },
                {
                    field: 'planName',
                    align: 'center',
                    title: '方案名称',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' type='text' name='topicPlanList[%s].planName' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'planType',
                    align: 'center',
                    title: '方案类型',
                    width: '100px',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' type='text' name='topicPlanList[%s].planType' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'doDate',
                    align: 'center',
                    width: '80px',
                    title: '间隔',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' type='text' name='topicPlanList[%s].doDate' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'doDateType',
                    align: 'center',
                    width: '80px',
                    title: '单位',
                    formatter: function(value, row, index) {
                       	var data = [{ index: index, type: value }];
    		            return $("#doDateTypeTpl").tmpl(data).html();
                    }
                },
                {
                    field: 'errorDate',
                    align: 'center',
                    title: '误差天数',
                    width: '80px',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' type='text' name='topicPlanList[%s].errorDate' value='%s'>", index, value);
                        return html;
                    }
                }]
            };
            $.table.init(options);
        });

        function addColumn() {
            var count = $("#" + table.options.id).bootstrapTable('getData').length;
            sub.editColumn();
        	
            $("#" + table.options.id).bootstrapTable('insertRow', {
                index: count,
                row: {
                    index: $.table.serialNumber(count),
                    planSort: "",
                    planType: "",
                    planName: "",
                    doDateType: "",
                    doDate: "",
                    errorDate: ""
        	    }
            });
        }
        
        function configPlan(){        	
        	var rows = $("#" + table.options.id).bootstrapTable('getSelections');
        	if (rows.length==0) {
    	        $.modal.alertWarning("请选择一个方案");
    	        return;
    	    }
        	if(rows.length>1){
        		$.modal.alertWarning("只能选择一个方案");
        		return;
        	}
        	var url = prefix + "/configPlan/"+$.table.selectColumns('planId');
			$.modal.open("选择项目", url);
        }
    </script>
</body>
</html>

<!-- 时间间隔类型 -->
<script id="doDateTypeTpl" type="text/x-jquery-tmpl">
<div>
<select class='form-control' name='topicPlanList[${index}].doDateType'>
    <option value="M" {{if type==="M"}}selected{{/if}}>月</option>
	<option value="Y" {{if type==="Y"}}selected{{/if}}>年</option>
	<option value="D" {{if type==="D"}}selected{{/if}}>年</option>
</select>
</div>
</script>